package com.system.connect.springboot_flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.connect.springboot_flowers.dto.FriendsDto;
import com.system.connect.springboot_flowers.entity.Friends;
import com.system.connect.springboot_flowers.entity.Privatemessages;
import com.system.connect.springboot_flowers.entity.Users;
import com.system.connect.springboot_flowers.service.IFriendsService;
import com.system.connect.springboot_flowers.service.IPrivatemessagesService;
import com.system.connect.springboot_flowers.service.IUsersService;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 好友表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/friends")
public class FriendsController {

    @Autowired
    private IFriendsService friendsService;

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IPrivatemessagesService privatemessagesService;

    /**
     * 添加好友
     */
    @PostMapping("/{friendUserId}")
    public R addFriend(@PathVariable Integer friendUserId) {
        System.out.println(friendUserId);
        Friends friends = new Friends();
        friends.setUserId(Integer.valueOf(TokenUtils.getUser()));
        friends.setFriendUserId(friendUserId);
        friends.setStatus("1");
        friendsService.save(friends);
        return R.success("添加成功");
    }

    /**
     * 获取好友列表
     */
    @GetMapping("/listByUserId")
    public R listByUserId() {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", TokenUtils.getUser());

        //好友列表
        ArrayList<Friends> friends = (ArrayList<Friends>) friendsService.list(queryWrapper);
        ArrayList<FriendsDto> friendsDtos = new ArrayList<>();
        //用户信息
        Users user = usersService.lambdaQuery()
                .eq(Users::getUserId, TokenUtils.getUser())
                .one();

        for (Friends friend : friends){
            FriendsDto friendsDto = new FriendsDto();
            Users friendUser = usersService.lambdaQuery()
                    .eq(Users::getUserId, friend.getFriendUserId())
                    .one();
            friendsDto.setFriendUserId(friendUser.getUserId());
            friendsDto.setFriendnickname(friendUser.getUsername());
            friendsDto.setFriendphoto(friendUser.getAvatarUrl());
            friendsDto.setUserId(user.getUserId());
            friendsDto.setUsernickname(user.getUsername());
            friendsDto.setUserphoto(user.getAvatarUrl());
            friendsDto.setUpdatedAt(friend.getCreatedAt());

            //最后一条消息
            QueryWrapper<Privatemessages> PrivatemessagesqueryWrapper = new QueryWrapper<>();
            //查询倒叙
            PrivatemessagesqueryWrapper.orderByDesc("created_at");
            PrivatemessagesqueryWrapper.eq("sender_id", friendUser.getUserId());
            PrivatemessagesqueryWrapper.eq("receiver_id", user.getUserId());
            PrivatemessagesqueryWrapper.last("limit 1");
            Privatemessages privatemessages = privatemessagesService.getOne(PrivatemessagesqueryWrapper);
            if (privatemessages != null){
                friendsDto.setLastMessage(privatemessages.getContent());
            }else {
                friendsDto.setLastMessage("已成功添加好友");
            }


            //好友关系状态
            if (friend.getStatus().equals("1")){
                friendsDto.setStatus("已添加");
            }else if (friend.getStatus().equals("0")){
                friendsDto.setStatus("未添加");
            }else {
                friendsDto.setStatus("已删除");
            }

            friendsDtos.add(friendsDto);
        }

        return R.success(friendsDtos);
    }

}
